import settings from '@/settings'
import cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/encrypted'
const identity= settings.identity
/**
 * 获取token
 * @returns 
 */
export function token(){
    return decrypt(window.sessionStorage.getItem(identity))||cookies.get(identity)
}

/**
 * 设置token
 * @param {string} key token
 * @param {boolean} remember 记住密码
 * @returns 
 */
export function setkey(key, remember){
    if(remember){
        cookies.set(identity, key, {expires: settings.expires})
    }
    window.sessionStorage.setItem(identity, encrypt(key))
}

/**
 * 清除 token
 */
export function clear(){
    cookies.remove(identity)
    window.sessionStorage.removeItem(identity)
}

/**
 * 获取缓存
 * @param {string} label 
 * @returns 
 */
export function objects(label){
    return window.sessionStorage.getItem(label)?JSON.parse(decrypt()):''
}

/**
 * 设置缓存
 * @param {string} label 
 * @param {string} object 
 */
export function setObjects(label, object){
    window.sessionStorage.setItem(label, encrypt(object))
}

